/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.upc.sad.controller;


import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JTextArea;
import pe.edu.upc.sad.model.CtrlEmpresa;
import pe.edu.upc.sad.model.CtrlUsuario;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.TooManyListenersException;
import pe.edu.upc.sad.dao.EmpresaDao;
import pe.edu.upc.sad.dao.EmpresaDaoImpl;
import pe.edu.upc.sad.dao.UsuarioDao;
import pe.edu.upc.sad.dao.UsuarioDaoImpl;
import pe.edu.upc.sad.util.Conexion;
import pe.edu.upc.sad.util.ToolSql;
/**
 *
 * @author casa
 */
public class LoginServlet extends HttpServlet {

    public CtrlUsuario ctrlUsuario = new CtrlUsuario();
    public CtrlEmpresa ctrlEmpresa = new CtrlEmpresa();
    JTextArea          outputArea  = new JTextArea();

    EmpresaDao comp = new EmpresaDaoImpl();
    UsuarioDao user = new UsuarioDaoImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
         processRequest(req, resp);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        processRequest(request, response);
    }
    private void processRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String destino = new String("");
        Connection conn = null;
        try{
            conn = Conexion.getConnection();

            String usu = req.getParameter("username")==null ? new String(""): (String) req.getParameter("username");
            String pass = req.getParameter("password")==null ? new String(""): (String) req.getParameter("password");

            if ( user.get(usu, pass) != null )
            {
                req.getSession().setAttribute("usuario", user.get(usu, pass));
                req.setAttribute("mensaje", "Login satisfactorio Usuario");
                destino = "/VerificarReserva.jsp";

            } else if ( comp.get(usu, pass) != null )
            {
                req.getSession().setAttribute("empresa", comp.get(usu, pass) );
                req.setAttribute("mensaje", "Login satisfactorio Empresa");
                destino = "/RegistroEmpresasAreas.jsp";

            } else {
                destino = "/index.jsp";
                req.setAttribute("mensaje", "Username no existe");
            }

            req.getRequestDispatcher(destino).forward(req, resp);

        }catch(Exception e)
        {
            System.out.println("Error en la pagina" + e.getMessage());
        }finally{
            try {
                ToolSql.close(null, null, null, conn);
            } catch (SQLException ex) {
                Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }


}
